//
//  AreaViewController.m
//  demo
//
//  Created by dllo on 15/7/11.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AreaViewController.h"
#import "AreaCollectionViewCell.h"
#import "AFNetworkHandler.h"
@interface AreaViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) UICollectionReusableView *header;
@property (nonatomic, retain) NSMutableArray *areaArr;
@end

@implementation AreaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createCollectionView];
    // Do any additional setup after loading the view.
}

- (void)getData
{
    NSString * str = @"http://chanyouji.com/api/destinations.json";
    [AFNetworkHandler GetWithAFNByURL:str completion:^(id result) {
        NSArray *tempArr = result;
        self.areaArr = [NSMutableArray array];
        for (NSDictionary *dic in tempArr) {
            NSArray *arr = [dic objectForKey:@"destinations"];
            NSArray *arrr = [Area arrayWithModelByArray:arr];
            for (Area *a in arrr) {
                [self.areaArr addObject:a];
            }
        }
        [self.collectionView reloadData];
    }];
}
- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //设置头部区域
    layout.headerReferenceSize = CGSizeMake(self.view.bounds.size.width, 50);
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
//    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.collectionView registerClass:[AreaCollectionViewCell class] forCellWithReuseIdentifier:@"cCell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    [_collectionView release];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AreaCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cCell" forIndexPath:indexPath];
    cell.area = self.areaArr[indexPath.item];
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.areaArr.count;
}
//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
//{
//    if (kind == UICollectionElementKindSectionHeader) {
//        self.header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
//        self.header.backgroundColor = [UIColor whiteColor];
//        UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"国内", @"国外"]];
//        seg.frame = CGRectMake(self.header.frame.origin.x + 30, self.header.frame.origin.y + 5, self.header.frame.size.width - 30, 30);
//        [self.header addSubview:seg];
//        [seg release];
//        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(seg.frame.origin.x, seg.frame.origin.y + seg.frame.size.height, self.header.frame.size.width, 1)];
//        view.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:241 / 255.0 alpha:1];
//        [self.header addSubview:view];
//        [view release];
//        return self.header;
//    } else {
//        return nil;
//    }
//}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(110, 30);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(collectionView.frame
                      .size.width, 50);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
